﻿using MSSClient.Model;
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using Zmy.Wpf.CMessageBox;

namespace MSSClient
{
    /// <summary>
    /// Interaction logic for SettingDlg.xaml
    /// </summary>
    public partial class SettingDlg : Window
    {

        private Boolean m_bDetectBarcode;
        private Boolean m_bDetectWhitePaper;

        public delegate void tiaoxingma_Delegate();
        public delegate void kongbai_Delegate();
        public delegate void saomiaoyuan_Delegate(int sourceListPos,long qualityValue);

        public tiaoxingma_Delegate tiaoxingma_DelegateObj;
        public kongbai_Delegate kongbai_DelegateObj;
        public saomiaoyuan_Delegate saomiaoyuan_DelegateObj;

/*
        public SettingDlg()
        {
            InitializeComponent();

            tiaoxingma_checkBox.IsChecked = false;
            kongbai_checkBox.IsChecked = false;

            //
            .Show(MSSWindow.m_bDetectBarcode + " " + MSSWindow.m_bDetectWhitePaper);

            if (MSSWindow.m_bDetectBarcode)
            {
                tiaoxingma_checkBox.IsChecked = true;
            }

            if (MSSWindow.m_bDetectWhitePaper)
            {
                kongbai_checkBox.IsChecked = true;
            }

            Loaded += SettingDlg_Loaded;
        }
*/

        public SettingDlg(Boolean bDetectBarcode, Boolean bDetectWhitePaper, IList<string> sourceNames, int sourceListPos,long qualityValue)
        {
           InitializeComponent();

           m_bDetectBarcode = bDetectBarcode;
           m_bDetectWhitePaper = bDetectWhitePaper;

           //MessageBox.Show(MSSWindow.m_bDetectBarcode + " " + MSSWindow.m_bDetectWhitePaper);
           //MessageBox.Show(m_bDetectBarcode + " " + m_bDetectWhitePaper);

           // if (MSSWindow.m_bDetectBarcode)
           if (m_bDetectBarcode)
           {
               tiaoxingma_checkBox.IsChecked = true;
           }

           // if (MSSWindow.m_bDetectWhitePaper)
           if (m_bDetectWhitePaper)
           {
               //kongbai_checkBox.IsChecked = true;
           }
            this.qualityValue.Text = qualityValue.ToString();

            Loaded += SettingDlg_Loaded;

            var sourceList = sourceNames;
            ManualSource.ItemsSource = sourceList;

            //if (sourceList != null && sourceList.Count > 0)
            if (sourceList != null && sourceList.Count > sourceListPos)
            {
                //MessageBox.Show("sourceListPos:" + sourceListPos + " sourceList[sourceListPos]" + sourceList[sourceListPos]);
                //MessageBox.Show("sourceListPos:" + sourceListPos);
                //ManualSource.SelectedItem = sourceList[sourceListPos];
                //ManualSource.SelectedValue = sourceList[sourceListPos];
                ManualSource.SelectedIndex = sourceListPos;
            }
            else if (sourceList != null && sourceList.Count > 0)
            {
                if (sourceListPos>= sourceList.Count)
                {
                    sourceListPos = 0;
                }
                ManualSource.SelectedItem = sourceList[sourceListPos];
            }
           // this.Closing += onClosing;
        }

        private void SettingDlg_Loaded(object sender, RoutedEventArgs e)
        {
            tiaoxingma_checkBox.Checked += tiaoxingma_Checked;
            tiaoxingma_checkBox.Unchecked += tiaoxingma_Checked;

            //kongbai_checkBox.Checked += kongbai_Checked;
            //kongbai_checkBox.Unchecked += kongbai_Checked;
        }

        private void tiaoxingma_Checked(object sender, RoutedEventArgs e)
        {
            tiaoxingma_DelegateObj();
        }

        private void kongbai_Checked(object sender, RoutedEventArgs e)
        {
            kongbai_DelegateObj();
        }

        private void queding_Button_Click(object sender, RoutedEventArgs e)
        {
            if (int.Parse(qualityValue.Text) < 50 || int.Parse(qualityValue.Text) > 80)
            {
                CMessageBox.Show("图像质量填写有误，请重新输入!", "提示信息", CMessageBoxButton.OKCancel, CMessageBoxImage.None, CMessageBoxDefaultButton.OK);
                return;
            }
            Global.getInstance().SourceListPos = ManualSource.SelectedIndex;
            Global.getInstance().qualityValue = long.Parse(qualityValue.Text);
            saomiaoyuan_DelegateObj(ManualSource.SelectedIndex, long.Parse(qualityValue.Text));
            //MessageBox.Show("ManualSource.SelectedIndex:" + ManualSource.SelectedIndex);
            this.Close();
        }

        private void onClosing(object sender, EventArgs e)
        {
            
        }

        private void ManualSource_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }
    }
}
